package jianzhi;

import niuke.TreeNode;

/**
 * description:
 * author:zt
 * date:2021-10-22
 */
//输入两棵二叉树A和B，判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
public class jianzhi26 {
//    public boolean isSubStructure(TreeNode A, TreeNode B) {
//        return isSame(A,B);
//    }
//    public boolean isSame(TreeNode A,TreeNode B){
//        if (A==null && B==null) return true;
//        else if (A==null || B==null) return false;
//
//        if (A.val != B.val) return isSame(A.left,B) || isSame(A.right,B);
//        else return isSame(A.left,B.left) || isSame(A.right,B.right);
//    }
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }
    boolean recur(TreeNode A, TreeNode B) {
        if(B == null) return true;
        if(A == null || A.val != B.val) return false;
        return recur(A.left, B.left) && recur(A.right, B.right);
    }
}
